<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add User Information</title>
    <style>
    body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    margin: 0;
    padding: 0;
    }

    h1 {
    text-align: center;
    color: #333;
    }

    form {
    max-width: 600px;
    margin: 20px auto;
    padding: 20px;
    background: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }

    label,
    input,
    select,
    textarea {
    display: block;
    margin-bottom: 15px;
    }

    input,
    select,
    textarea {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 5px;
    }

    button {
    display: block;
    width: 100%;
    padding: 10px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    }

    button:hover {
    background-color: #45a049;
    }
    </style>


</head>
<body>
<h1>Add User Information</h1>
<form action="/mananger/addUserMessage" method="post">
    <label for="realName">Real Name:</label>
    <input type="text" id="realName" name="realName" required><br>

    <label for="birthday">Birthday:</label>
    <input type="date" id="birthday" name="birthday" required><br>

    <label for="sex">Sex:</label>
    <select id="sex" name="sex" required>
        <option value="1">女</option>
        <option value="2">男</option>

    </select><br>

    <label for="cardId">Card ID:</label>
    <input type="text" id="cardId" name="cardId" required><br>

    <label for="address">Address:</label>
    <textarea id="address" name="address" required></textarea><br>

    <label for="phone">Phone:</label>
    <input type="tel" id="phone" name="phone" required><br>

    <label for="mail">Email:</label>
    <input type="email" id="mail" name="mail" required><br>

    <label for="password">Password:</label>
    <input type="password" id="password" name="password" required><br>

    <label for="houseNumber">houseNumber:</label>
    <input type="text" id="houseNumber" name="houseNumber" required><br>

    <label for="userType">User Type:</label>
    <select id="userType" name="userType" required>
        <option value="1">用户</option>
        <option value="2">员工</option>
        <option value="3">管理员</option>
    </select><br>

    <label for="userStatus">User Status:</label>
    <select id="userStatus" name="userStatus" required>
        <option value="1">正常</option>
        <option value="2">假删除</option>

    </select><br>

    <button type="submit">Submit</button>
</form>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        document.getElementById('userForm').addEventListener('submit', function(event) {
            event.preventDefault();  // 阻止表单默认提交行为

            const formData = new FormData(this);

            fetch('/mananger/addUserMessage', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(Object.fromEntries(formData))  // 将 FormData 转换为对象并发送到服务器
            })
                .then(response => response.json())
                .then(result => {
                    if (result.success) {
                        alert("success!")
                        window.location.href = '/addUser';  // 重定向到成功页面

                    } else {
                        alert(result.error);  // 显示错误消息
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('Failed to add user information. Please try again.');
                });
        });
    });

</script>

</body>
</html>